package com.woniuxy.his.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.his.common.Result;
import com.woniuxy.his.exception.ReturnNoResultExceptionHandle;
import com.woniuxy.his.service.IPatientService;
import com.woniuxy.his.vo.PatientByCheckeIdVo;
import com.woniuxy.his.vo.PatientByprescriptionIdVo;
import com.woniuxy.his.vo.PayOfPrescriptionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.his.vo.GuaHaoVo;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 病人表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@Slf4j
@CrossOrigin("*")
@RestController
public class PatientController {

    @Resource
    private IPatientService patientService;

    @GetMapping("queryMedicining")
    public Result queryMedicining(String isGiveMedicine, @RequestParam(name = "current", defaultValue = "1") Integer current, @RequestParam(name = "pageSize", defaultValue = "3") Integer pageSize, String startTime, String stopTime) throws ParseException {
        log.info("是否取药：{}，当前页：{}，页容量：{}", isGiveMedicine, current, pageSize);
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time1 = sdf.parse(startTime);
        Date time2 = sdf.parse(stopTime);
        IPage<PayOfPrescriptionVo> patientsByPageAndStatu = patientService.findPatientByPageAndStatu(isGiveMedicine, current, pageSize, time1, time2);
        return Result.success("20000", "查询成功", patientsByPageAndStatu);
    }

    @GetMapping("queryMedicined")
    public Result queryMedicined(String isGiveMedicine, @RequestParam(name = "current", defaultValue = "1") Integer current, @RequestParam(name = "pageSize", defaultValue = "3") Integer pageSize, String startTime, String stopTime) throws ParseException {
        log.info("是否取药:{}，当前页:{}，页容量:{}", isGiveMedicine, current, pageSize);
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date time1 = simpleDateFormat.parse(startTime);
        Date time2 = simpleDateFormat.parse(stopTime);
        IPage<PayOfPrescriptionVo> patientsByPageAndStatu = patientService.findPatientByPageAndStatu(isGiveMedicine, current, pageSize, time1, time2);
        return Result.success("20000", "查询成功", patientsByPageAndStatu);
    }

    @GetMapping("getMsgOfOne")
    public Result getMsgOfOne(Integer prescriptionId) {
        log.info("处方id：{}", prescriptionId);
        if (prescriptionId == null) {
            return Result.fail("40005", "参数为空", null);
        }
        try {
            PatientByprescriptionIdVo msgOfOne = patientService.getMsgOfOne(prescriptionId);
            return Result.success("20000", "success", msgOfOne);
        } catch (ReturnNoResultExceptionHandle e) {
            log.warn("返回为null{}", e);
            return Result.fail("40000", "空结果返回", null);
        } catch (Exception e) {
            log.error("系统异常{}", e);
            return Result.fail("40005", "系统异常", null);
        }

    }

    @GetMapping("getMsgOfOneByCheckId")
    public Result getMsgOfOneByCheckId(Integer checkId) {
        log.info("检查项目id{}", checkId);
        if (checkId == null) {
            return Result.fail("40000", "空参数！", null);
        }
        try {
            PatientByCheckeIdVo msgOfOneByCheckId = patientService.getMsgOfOneByCheckId(checkId);
            return Result.success("20000", "查询成功！", msgOfOneByCheckId);
        } catch (ReturnNoResultExceptionHandle e) {
            log.warn("返回为null{}", e);
            return Result.fail("40000", "空结果返回", null);
        } catch (Exception e) {
            log.error("系统异常{}", e);
            return Result.fail("40005", "系统异常", null);
        }

    }

    //项目划价中根据挂号查询病人信息
    @GetMapping("/searchPatientByProject")
    public Result searchPatientByProject(Integer input){
        log.info("挂号单号={}",input);
        GuaHaoVo patient = patientService.searchPatientByProject(input);
        return patient==null?Result.fail("0000","未找到",null):Result.success("1111","找到了",patient);
    }
}

